package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Map;
import java.util.Set;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

	BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

	@Autowired
	UserDao userDao;

	@Autowired
	RoleDao roleDao;

	@Autowired
	PermissionDao permissionDao;

	/**
	 * 1. 在mybatis中实现一对多的查询
	 * 2. 根据用户名查询用户对象
	 * 根据用户id查询角色对象
	 * 根据每个角色的id查询相应的权限对象
	 * 组合数据
	 *
	 * @param username
	 * @return
	 */
	@Override
	public SysUser findByUsername(String username) {
		//根据用户名查询用户对象
		SysUser sysUser = userDao.findByUsername(username);
		//根据用户id查询角色对象
		Set<Role> roleSet = roleDao.findRolesByUserId(sysUser.getId());
		//根据每个角色的id查询相应的权限对象
		for (Role role : roleSet) {
			Set<Permission> permissionSet = permissionDao.findPermissionsByRoleId(role.getId());
			//把权限列表添加到角色中
			role.setPermissions(permissionSet);
		}
		//把角色列表添加到用户对象中
		sysUser.setRoles(roleSet);
		return sysUser;
	}

	/*根据条件查询分页*/
	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		//1.开始分页
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		//2.根据条件查询
		Page<Map<String, Object>> page = userDao.findByCondition(queryPageBean.getQueryString());
		for (Map<String, Object> map : page) {
			try {
				String birthdayStr = String.valueOf(map.get("birthday"));
				map.put("birthday", birthdayStr);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//3.创建PageResult对象
		PageResult pageResult = new PageResult(page.getTotal(), page);
		return pageResult;
	}

	/*添加会员*/
	@Override
	public void add(Integer[] roleIds, SysUser sysUser) {
		//密码加密
		String encode = passwordEncoder.encode(sysUser.getPassword().trim());
		sysUser.setPassword(encode);
		userDao.add(sysUser);
		if (sysUser.getId() != null && roleIds != null && roleIds.length > 0) {
			for (Integer roleId : roleIds) {
				//维护会员和角色(sysUser,role)
				userDao.set(sysUser.getId(), roleId);
			}
		}
	}

	/*删除用户*/
	@Override
	public void deleteById(Integer id) {
		//根据userid查询和role的关联
		Long count = userDao.findRoleCountsByUserId(id);
		if (count > 0) {
			userDao.deleteAssociation(id);//用户和角色，sysUser,role
		}
		userDao.deleteById(id);
	}

	/*编辑中数据回显，根据id查询用户*/
	@Override
	public SysUser findById(Integer id) {
		return userDao.findById(id);
	}

	/*根据userId查询roleIds(即user和role的关联)*/
	@Override
	public Integer[] findRoleIdsByUserId(Integer id) {
		return userDao.findRoleIdsByUserId(id);
	}

	/*编辑用户*/
	@Override
	public void edit(Integer[] roleIds, SysUser sysUser) {
		userDao.edit(sysUser);
		userDao.deleteAssociation(sysUser.getId());
		if (roleIds != null && roleIds.length >= 0) {
			for (Integer roleId : roleIds) {
				userDao.set(sysUser.getId(), roleId);
			}
		}
	}
}
